package com.lx.mailweb.controller;

import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
//import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.lx.mailweb.biz.UserBiz;
import com.lx.mailweb.domain.User;

@Controller
/*@RequestMapping("/user")*/
public class UserController {

	@Resource
	private UserBiz userBiz;

	// @ExceptionHandler
	public String exp(Exception ex, HttpServletRequest request) {
		request.setAttribute("ex", ex);
		return "error";
	}

	// 后台主页
	@RequestMapping("/main")
	public String main(HttpServletRequest request) {

		User admin = (User) request.getSession().getAttribute("admin");
		if (admin == null) {
			return "redirect:admin/login";
		}

		return "/admin/main";
	}

	// 登录页面
	@RequestMapping(value = "/admin/login", method = RequestMethod.GET)
	public String tologin() {

		return "admin/login";
	}

	// 登录
	@RequestMapping(value = "/admin/login", method = RequestMethod.POST)
	public String login(String username, String password,
			HttpServletRequest request) {
		User admin = userBiz.login(username, password);
		if (admin != null) {
			HttpSession session = request.getSession(true);
			session.setAttribute("admin", admin);
			return "redirect:/main";
		} else {
			request.setAttribute("error", "用户不存在或者密码错误");
			return "admin/login";
		}
	}

	// 注销
	@RequestMapping("/admin/loginout")
	public String loginOut(HttpServletRequest request) {
		HttpSession session = request.getSession(true);// 放置创建session
		if (session != null) {
			session.removeAttribute("admin");
		}
		return "redirect:login";
	}

	/*// 用户管理
	@RequestMapping(value = "/manage", method = RequestMethod.GET)
	public String manage(Integer page, Integer pageSize,
			HttpServletRequest request) {
		User admin = (User) request.getSession().getAttribute("admin");
		if (admin != null) {
			PageModel pageModel = userBiz.getUser(pageSize, page);
			request.setAttribute("pageModel", pageModel);
			return "admin/user/user";
		}
		return null;
	}

	// 增加用户页面
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String save(HttpServletRequest request) {
		return "admin/user/saveUser";
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String saveUser(User user, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			User admin = (User) request.getSession().getAttribute("admin");
			if (admin != null) {
				if (userBiz.saveUser(user))
					out.print("success");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public String delUser(int[] ids, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			User admin = (User) request.getSession().getAttribute("admin");
			if (admin != null) {
				if(userBiz.delUser(ids))
				out.print("success");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping("/edit")
	public String edit(Integer id, HttpServletRequest request) {
		User admin = (User) request.getSession().getAttribute("admin");
		if (admin != null) {
			User user = userBiz.getUserById(id);
			if (user != null) {
				request.setAttribute("member", user);
			}

			return "admin/user/saveUser";
		}
		return null;
	}*/
}